<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Model\TaoCang;
use App\Repository\Contracts\GoodsInterface;
use App\Repository\Contracts\GoodsJavaInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 19:26
 *
 * @Description: 商品
 */
class GoodsController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var GoodsInterface
     */
    protected $goods;

    /**
     * @var GoodsJavaInterface
     */
    protected $goodsJava;

    protected $uid;

    /**
     * GoodsController constructor.
     * @param Request $request
     * @param GoodsInterface $goods
     * @param GoodsJavaInterface $goodsJava
     */
    public function __construct(Request $request, GoodsInterface $goods, GoodsJavaInterface $goodsJava)
    {
        $this->request = $request;

        $this->goods = $goods;

        $this->goodsJava = $goodsJava;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $articleNo = $this->request->input('articleNo', null);
        $barCode = $this->request->input('barCode', null);
        $name = $this->request->input('name', null);
        $mnemonic = $this->request->input('mnemonic', null);

        //Search Map
        $searchMap = $this->searchMap([
            ['article_no', $articleNo, '='],
            ['bar_code', $barCode, '='],
            ['zh_name', $name, 'like'],
            ['en_name', $name, 'like'],
            ['mnemonic', $mnemonic, '=']
        ]);
        $list = $this->goods->lists($limit, $page, $searchMap);


        return $this->success($list);
    }
    public function taocangList(){

        $taocang = TaoCang::get();
        return $this->success($taocang);

    }
    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->goods->detail($id);

        return $this->success($detail);
    }

    /**
     * 根据[条形码, 助记码, 货号]返回商品详情
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function findBy()
    {
        $keywords = $this->request->input('keywords', null);
        if ($keywords) {
            $map = $this->searchMap([
                ['bar_code', $keywords, '='],
                ['article_no', $keywords, '='],
                ['mnemonic', $keywords, '='],
                ['goods_id', $keywords, '='],
            ]);
            $detail = $this->goods->detailBy($map);

            return $this->success($detail);
        }

        return $this->fail('非法操作');
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {

        try {
//            $data = $this->request->only(['category_id','taocang_id ', 'article_no', 'bar_code', 'zh_name', 'en_name', 'mnemonic', 'brand', 'tax', 'price', 'inventory', 'status']);
            $data = $this->request->input();

            $id = $this->goods->created($data);
            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Update
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated($id)
    {
        try {
            $data = $this->request->only(['category_id','taocang_id ','article_no', 'bar_code', 'zh_name', 'en_name', 'mnemonic', 'brand', 'tax', 'price', 'inventory', 'status']);
            $data = $this->request->input();
            $result = $this->goods->updated($id, $data);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateStatus($id)
    {
        try {
            $status = $this->request->input('status');
            $result = $this->goods->updateStatus($id, $status);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function initGoods()
    {
        if (!env('DIRECT_SALE') || !env('JAVA_NETWORK')) {
            return $this->success();
        }

        try {
            $detail = $this->goods->findIdForFirst();
            $result = $this->goodsJava->syncList(isset($detail[0])?$detail[0]['created_at']:0);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}